package com.irun.sm.ui.demo.ui;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.irun.sm.ui.demo.utils.ActivityUtils;
import com.irun.sm.ui.demo.view.MyHorizontalScrollView;
import com.irun.sm.ui.demo.view.MyHorizontalScrollView.SizeCallBack;

/***
 * @author huangsm
 * @date 2013-4-1
 * @email huangsanm@gmail.com
 * @desc 横向滑动activity
 */
public class HorizontalActivity extends Activity implements OnClickListener {

	private MyHorizontalScrollView mHorizontalScrollView;
	private ListView mMenuListView;
	private LinearLayout mAppLinearLayout;
	private ImageButton mSlideImageButton;
	
	private boolean isSlided = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		ActivityUtils.requestNotTitleBar(this);
		
		setContentView(R.layout.horizontal_layout);
		
		setupView();
	}
	
	private void setupView(){
		final LayoutInflater inflater = getLayoutInflater();
		//右侧导航
		mMenuListView = (ListView) inflater.inflate(R.layout.horiz_menu, null);
		String[] str = new String[10];
		for (int i = 0; i < 10; i++) {
			str[i] = "Item " + i;
		}
		mMenuListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, str));
		
		mAppLinearLayout = (LinearLayout) inflater.inflate(R.layout.horiz_app, null);
		//滚动条
		mHorizontalScrollView = (MyHorizontalScrollView) findViewById(R.id.scroll_view);
		mSlideImageButton = (ImageButton) mAppLinearLayout.findViewById(R.id.btn_menu);
		mSlideImageButton.setOnClickListener(this);
		mHorizontalScrollView.resetView(new View[]{mMenuListView, mAppLinearLayout}, 1, new MenuSizeCallBack(mSlideImageButton));
	}
	
	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.btn_menu){
			int menuWidth = 0;
			mMenuListView.setVisibility(View.VISIBLE);
			if(!isSlided){
				menuWidth = 0;
				mHorizontalScrollView.smoothScrollTo(menuWidth, 0);
			} else {
				menuWidth = mMenuListView.getMeasuredWidth();
				mHorizontalScrollView.smoothScrollTo(menuWidth, 0);
			}
			isSlided = !isSlided;
		}
	}
	
	/**
	 * @author huangsm
	 * 测量menu显示时的width
	 */
	class MenuSizeCallBack implements SizeCallBack {

		private ImageButton mImageButton;
		private int mSlideButtonWidth;
		public MenuSizeCallBack(ImageButton btn){
			mImageButton = btn;
		}
		
		@Override
		public void onMeasureView() {
			mSlideButtonWidth = mImageButton.getMeasuredWidth() + 20;
		}

		@Override
		public void getViewSize(int index, int w, int h, int[] dims) {
			dims[0] = w;
			dims[1] = h;
			if(index == 0){
				dims[0] = w - mSlideButtonWidth;
			}
		}
	}
}
